package Controleur;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Date;

import javax.swing.Timer;


public class Topeur implements ActionListener, Serializable {

	private ControllerSimulation controlleur;
	private Timer top;
	private int delay;
	private int tempsReelTour;
	
	
	private Date tourFin;
	public Topeur(ControllerSimulation c) {
		controlleur = c;
		delay = 100;
		top = new Timer(delay, this);
		
		tourFin = new Date();

	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Date tourDebut = new Date();
		
		if (controlleur.isSimulationFinished())
		{
			this.stop();
			controlleur.terminerSimulation();
		}
		else
		{
			tourDebut = new Date();
			controlleur.jouerTour();			
			tourFin = new Date();
			tempsReelTour = (int) (tourFin.getTime() - tourDebut.getTime());
		}
		if ((delay-15) < tempsReelTour)
			setDelay(tempsReelTour + 20);
	}
	
	public int getTempsReelTour() {
		return tempsReelTour;
	}
	
	public void start()
	{
		top.start();
	}
	
	public void stop()
	{
		top.stop();
	}
	
	public int getDelay()
	{
		return delay;
	}
	
	public void setDelay(int delay) {
		if (delay >= 10)
		{
			top.stop();
			this.delay = delay;
			top.setDelay(delay);
			top.start();
		}
	}
	
	public Timer getTimer()
	{
		return top;
	}


}
